Ext.onReady(function(){
	var Tree = Ext.tree;
	var myTreeLoader = new Tree.TreeLoader({
	    dataUrl : "treeNode.action"
	});
	myTreeLoader.on("beforeload", function(myTreeLoader, node) {
		if(node.id!="1"){
			myTreeLoader.baseParams={
					path: node.attributes.path.path,//父节点id
					method:"POST"
					};
		} else {
			myTreeLoader.baseParams={
					path: node.attributes.path,//父节点id
					method:"POST"
					};
		}
		
	});
	var tree = new Ext.tree.TreePanel({
	    el:'tree',
	    autoScroll : true,
	    animate : true,
	    enableDD : true,
	    height: 550,
	    width : 400,
	    containerScroll : true,
	    loader : myTreeLoader,
	    contextMenu : new Ext.menu.Menu({
	        items: [{
	            id: 'set-password',
	            text: '设置密码'
	        }],
	        listeners: {
	            itemclick: function(item) {
	                switch (item.id) {
	                    case 'set-password':
	                    	if(admin == 0){
	                    		Ext.Msg.alert('错误', '您不是管理员,无法设置密码!');
	                    		return ;
	                    	}
	                        var node = item.parentMenu.contextNode;
	                        if (node.parentNode) {
	                    		var pwdText = new Ext.form.TextField({
	                    			autoWidth	: 'true',
	                    			layout		: 'fit',
	                    			name		: 'password',
	                    			inputType	: 'password',
	                    			fieldLabel: '密码:'
	                    		}); 
	                    		var passwordWin = new Ext.Window({ 
	                    			title	  	: '请设置没密码', 
	                    			xtype 		: 'form',
	                    			layout		: 'fit',
	                    			modal	  	: true, 
	                    			plain	  	: true,
	                    			item		: pwdText,
	                    			buttons: ['密码:',pwdText,
	                    			          {text:"确定", handler:function(){
	                    			        	  Ext.Ajax.request({
	                    			        		   url: 'setFloderPassword.action',
	                    			        		   success: function(response, opts){
	                    			        			  var result = Ext.decode(response.responseText);
	                    			        			  if(result.success==true){
	                    			        				  Ext.Msg.alert('信息', result.msg);
	                    			        			  } else {
	                    			        				  Ext.Msg.alert('错误', '密码设置失败!');
	                    			        			  }
	                    			        		   },
	                    			        		   failure: function(response, opts){
	                    			        			   Ext.Msg.alert('错误', '交互出错,请联系管理员!');
	                    			        		   },
	                    			        		   params: { 
	                    			        			   floder	: node.attributes.text,
	                    			        			   password	: pwdText.getValue()
	                    			        		   }
	                    			        		});
	                    			        	  passwordWin.close();
	                    			          }}
	                    	        ]
	                    		});
	                    		passwordWin.show(tree);
	                        }
	                        break;
	                }
	            }
	        }
	    })
	});
	tree.on('click', function(node){
		if(node.id == '1') {
			store.reload({params:{path:node.attributes.path}});
			dataview.refresh();
			return ;
		}
		var pwdText = new Ext.form.TextField({
			autoWidth	: 'true',
			layout		: 'fit',
			name		: 'password',
			inputType	: 'password',
			fieldLabel: '密码:'
		}); 
		var passwordWin = new Ext.Window({ 
			title	  	: '请输入密码', 
			xtype 		: 'form',
			layout		: 'fit',
			modal	  	: true, 
			plain	  	: true,
			item		: pwdText,
			buttons: ['密码:',pwdText,
			          {text:"确定", handler:function(){
			        	  //Ext.Msg.alert('验证密码',pwdText.getValue());
			        	  Ext.Ajax.request({
			        		   url: 'floderPassword.action',
			        		   success: function(response, opts){
			        			  var result = Ext.decode(response.responseText);
			        			  if(result.success==true){
			        				  Ext.Msg.alert('信息', result.msg);
			        				  if(node.id!='1'){
			        						//alert(node.attributes.path.path);
			        						store.reload({params:{path:node.attributes.path.path}});
			        					    dataview.refresh();
			        					}
			        					else {
			        						//alert(node.attributes.path);
			        						store.reload({params:{path:node.attributes.path}});
			        						dataview.refresh();
			        					}
			        			  } else {
			        				  Ext.Msg.alert('密码错误', '密码错误!');
			        				  store.removeAll();
			        			  }
			        		   },
			        		   failure: function(response, opts){
			        			   Ext.Msg.alert('错误', '交互出错,请联系管理员!');
			        			   store.removeAll();
			        		   },
			        		   params: { 
			        			   floder	: node.attributes.text,
			        			   password	: pwdText.getValue() 
			        		   }
			        		});
			        	  passwordWin.close();
			          }}
	        ]
		}); 
		passwordWin.show(tree);
		/*if(node.id!='1'){
			//alert(node.attributes.path.path);
			store.reload({params:{path:node.attributes.path.path}});
		    dataview.refresh();
		}
		else {
			//alert(node.attributes.path);
			store.reload({params:{path:node.attributes.path}});
			dataview.refresh();
		}*/
	});
	tree.on('contextmenu',function(node, e) {
        node.select();
        var c = node.getOwnerTree().contextMenu;
        c.contextNode = node;
        c.showAt(e.getXY());
    });
	
	var root = new Tree.AsyncTreeNode( {
		text : "点读笔音频总目录",
		draggable : false,
		id : "1",
		path : "d:\\site\\1916_naiyumi_082354\\ROOT\\voice"
		//path : "D:\\My documents\\My Music"
	});
	tree.setRootNode(root);
	tree.render();
	root.expand();
	
	var proxy= new Ext.data.HttpProxy({
		method: 'POST',
	    prettyUrls: false,
	    url: 'treeNodeClick.action'
	});
	
	var store = new Ext.data.JsonStore({
		storeId: 'storeTest',
		root: 'rows',
		idProperty: 'id',
		fields : [{name:'id',type:'int'}, 'name', {name:'path',type:'string'}],
		proxy : proxy,
		sortInfo: {
		    field    : 'name',
		    direction: 'ASC'
		}
	});
	store.load();
	
	var selectText = new Ext.form.TextField({
		name: 'selectText',
		fieldLabel: '文件名'
	});
	
	var selectButton = new Ext.Button({
		text: '搜索',
		handler: function() {
			if(selectText.getValue() == ''){
				dataview.bindStore(store);
			} else {
				var textValue = selectText.getValue();
				var selectStore = new Ext.data.JsonStore({
					root: 'rows',
					idProperty: 'id',
					fields : [{name:'id',type:'int'}, 'name', {name:'path',type:'string'}]
				});
				for(var i=0;i<store.getCount();i++){
					var nameString = store.getAt(i).get('name');
					nameString = nameString.toLowerCase();
					if(nameString.indexOf(textValue) > -1){
						selectStore.add(store.getAt(i));
					}
				}
				dataview.bindStore(selectStore);
			}
	    }
	});

	var dataview = new Ext.DataView({
	    store: store,
	    tpl  : new Ext.XTemplate(
	        '<ul>',
			'<tpl for=".">',
			'<li class="phone">',
//			'<img width="64" height="64" src="jsp/protect/images/phones/phone{[values.id]}.png" />',
			'<a href="{path}" target="_self" style="text-decoration: none;">',
			'<img width="64" height="64" src="pic/{name}.jpg" />',
//			'<a href="fileDownload.action?filePath={path}" target="_self"><strong>{name}</strong><a>',
			'<strong>{name}</strong><a>',
			'</li>',
			'</tpl>',
			'</ul>'
	    ),
		id: 'phones',
		itemSelector: 'li.phone',
		overClass   : 'phone-hover',
	    singleSelect: false,
	    multiSelect : false,
	    autoScroll  : true
	});

	var panel = new Ext.Panel({
	    title: '点击音频下载',
		layout: 'fit',
		tbar: [selectText,selectButton],
		items : [dataview],
		height: 550,
		width : 800,
		renderTo: 'panel'
	});
});